package com.han.shadow.customerapp.fragment;

import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.adapter.MyGoodsesAdapter;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.bean.Order;
import com.han.shadow.customerapp.http.Connector;
import com.han.shadow.customerapp.mystatic.MyScreen;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ACE on 2016/4/23 0023.
 */
public class OrderInfoFragment extends DialogFragment{


    private Connector conn = new Connector();

    private final List<Goods> goodses;
    private final Context parent;
    private View view = null;

    private ListView lv = null;
    private BaseAdapter adapter = null;

    private List<List<Bitmap>> bitmaps = new ArrayList<>();
    private List<Goods> goodsesInOrder = new ArrayList<>();

    private Order order;


    int w;
    int h;


    public OrderInfoFragment(Order order,List<Goods> goodses,Context context){

        this.order = order;
        this.goodses = goodses;
        this.parent = context;

        w = MyScreen.dm.widthPixels;
        h = MyScreen.dm.heightPixels;
        //w = (int)(w-w*0.1);
        h = (int)(h-h*0.1);
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();

        window.setLayout(w,h);
        window.setGravity(Gravity.CENTER);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.fragment_orderinfo,null);

        lv = (ListView)view.findViewById(R.id.orderinfo_list);
        if(order != null && goodses != null){

            for(Map.Entry<String,Integer> entry: order.getGoodsIDs().entrySet()){
                for(Goods goods : goodses){
                    if(goods.getGid().equals(entry.getKey())){
                        goodsesInOrder.add(goods);
                        break;
                    }
                }
            }

            adapter = new MyGoodsesAdapter(goodsesInOrder,inflater,parent);
            lv.setAdapter(adapter);
            getPic();
        }

        return view;
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){//更新图片
                //adapter.update(bitmaps);
                updateImage();
            }
        }
    };



    public void updateImage(){
        int  visiblePostion = lv.getFirstVisiblePosition();
        int visibleLastPostion = lv.getLastVisiblePosition();
        for(int i  = 0; i <= visibleLastPostion; i++){
            View view = lv.getChildAt(i-visiblePostion);

            ImageView iv = (ImageView)view.findViewById(R.id.image_gs_goods);
            iv.setImageBitmap(bitmaps.get(i).get(0));
        }
    }
    public void getPic(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(Goods goods:goodsesInOrder){
                    List<Bitmap> bs = new ArrayList<Bitmap>();
                    for(String pic:goods.getPicPath()){
                        bs.add(conn.queryPic(pic));
                    }
                    bitmaps.add(bs);
                }
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        }).start();
    }
}
